package com.zhuozhou.jdyc.main.user.viewmodel;

import android.app.Application;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.RegexUtils;
import com.zhuozhou.jdyc.base.BaseViewModel;
import com.zhuozhou.jdyc.main.main.bean.BasicsCitySearchBean;
import com.zhuozhou.jdyc.main.user.bean.RequestLoginBean;
import com.zhuozhou.jdyc.main.user.bean.UserInfo;
import com.zhuozhou.jdyc.main.user.view.UserView;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Create by CherishTang on 2020/3/19 0019
 * describe:loginViewModel
 */
public class UserViewModel extends BaseViewModel<UserView> implements TextWatcher {
    TimeCount timer;

    public UserViewModel(@NonNull Application application) {
        super(application);
        setShowDialog(false);
    }

    public void loginClick(View v, RequestLoginBean requestLoginBean) {
        loginClick(v, requestLoginBean, null);
    }

    public void loginClick(View v, RequestLoginBean requestLoginBean, String password) {
        int id = v.getId();
//        if (id == R.id.tv_login_way_switch) {
//            TextView tvLoginWaySwitch = (TextView) v;
//            baseView.showLoading("切换中...");
//            new Handler().postDelayed(() -> {
//                baseView.hideLoading();
//                if (v.getContext().getResources().getString(R.string.login_way_code).equals(tvLoginWaySwitch.getText().toString())) {
//                    tvLoginWaySwitch.setText(R.string.login_way_password);
//                    baseView.switchLoginWay(false);
//                } else {
//                    tvLoginWaySwitch.setText(R.string.login_way_code);
//                    baseView.switchLoginWay(true);
//                }
//            }, new Random().nextInt(3000));
//        } else if (id == R.id.tv_forget_password) {
////            ForgetPasswordActivity.show(this, new Bundle());
//            baseView.showToast("忘记密码");
//        } else if (id == R.id.phone_number_button) {
//            //判断未填写
//            if (StringUtil.isEmpty(requestLoginBean.getMobile())) {
//                baseView.showToast("您还没有输入内容");
//                return;
//            }
//            //判断号段存在不存在
//            if (!RegexUtils.isMobile(requestLoginBean.getMobile())) {
//                baseView.showToast("您输入的手机号段不存在或位数不正确");
//                return;
//            }
//            timer.start();
//            ((TextView) v).setTextColor(ContextCompat.getColor(v.getContext(), R.color.btn_textcolor_unselected));
//            baseView.hideKeyboard();
//        } else
        if (id == R.id.login_submit) {
            if (StringUtil.isEmpty(requestLoginBean.getMobile())) {
                ToastUtils.showShort(v.getContext(), "请填写手机号码");
                return;
            }
            if (!RegexUtils.isMobile(requestLoginBean.getMobile())) {
                ToastUtils.showShort(v.getContext(), "手机号码不正确");
                return;
            }
            if (StringUtil.isEmpty(password)) {
                ToastUtils.showShort(v.getContext(), "请填写密码");
                return;
            }
            baseView.showLoading("正在登录，请稍后...");
            this.loginPass(requestLoginBean.getMobile(), password);
        }
    }

    public MutableLiveData<UserInfo> BasicsDelShopMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<BasicsCitySearchBean> RegisterMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<BasicsCitySearchBean> LoginCodeMutableLiveData = new MutableLiveData<>();

    public void register(String phone) {
        Map map = new HashMap();
        map.put("phone", phone);
        observe(apiServiceHelper.sms(map), true, RegisterMutableLiveData);
    }

    public void loginCode(String phone, String yzm, String pass) {
        Map map = new HashMap();
        map.put("phone", phone);
        map.put("pass", pass);
        map.put("yzm", yzm);
        observe(apiServiceHelper.register(map), true, LoginCodeMutableLiveData);
    }

    public void loginPass(String phone, String pass) {
        Map map = new HashMap();
        map.put("phone", phone);
        map.put("pass", pass);
        observe(apiServiceHelper.login(map), true, BasicsDelShopMutableLiveData);
    }


    /**
     * 获取验证码倒计时
     */
    class TimeCount extends CountDownTimer {
        TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onFinish() {//计时完毕时触发
            baseView.countDown("重新获取", true, R.color.white);
        }

        @Override
        public void onTick(long millisUntilFinished) {//计时过程显示
            baseView.countDown("重新发送(" + millisUntilFinished / 1000 + ")秒", false, R.color.btn_textcolor_unselected);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (RegexUtils.isMobile(s.toString())) {
            baseView.countDown(null, true, R.color.white);
        } else {
            baseView.countDown(null, false, R.color.btn_textcolor_unselected);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

}
